
# TODO : Review if all flags are correct after creating GCC Cross comnpiler 

PROJDIRS := core kio
SRCFILES := $(shell find $(PROJDIRS) -type f -name "*.c")
HDRFILES := $(shell find $(PROJDIRS) -type f -name "*.h")
ASMFILES := $(shell find $(PROJDIRS) -type f -name "*.asm")
ALLFILES := $(SRCFILES) $(HDRFILES) $(ASMFILES)
OBJFILES := $(patsubst %.c,%.c.o,$(SRCFILES))
ASMOBJFILES := $(patsubst %.asm,%.asm.obj,$(ASMFILES))
DEPFILES := $(patsubst %.c,%.dep,$(SRCFILES)) $(patsubst %.asm,%.dep,$(ASMFILES))

.PHONY: all clean dist check todolist

WARNINGS := -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align \
			-Wwrite-strings -Wmissing-prototypes -Wmissing-declarations \
			-Wredundant-decls -Wnested-externs -Winline -Wno-long-long \
			-Wuninitialized -Wconversion -Wstrict-prototypes
CFLAGS := -I./ -g -std=c99 -m32 -nostdlib -nostartfiles -nodefaultlibs $(WARNINGS)

AS := nasm
ASMFLAGS := 

all: KERNEL.SYS

KERNEL.SYS: $(OBJFILES) $(ASMOBJFILES)
	@ld -o KERNEL.SYS $(OBJFILES) $(ASMOBJFILES) -T link.ld -m elf_i386

clean:
	-@$(RM) $(wildcard $(OBJFILES) $(ASMOBJFILES) $(DEPFILES) KERNEL.SYS)
	
-include $(DEPFILES)

todolist:
	-@for file in $(ALLFILES); do fgrep -H -e TODO -e FIXME $$file; done; true

%.c.o: %.c Makefile
	@$(CC) $(CFLAGS) -MMD -MP -c $< -o $@

%.asm.obj: %.asm Makefile
	@$(AS) -o $@ -MD $(patsubst %.asm,%.dep,$<) -f elf $<
